package com.green.voluntaryfiling.controller;

import com.green.voluntaryfiling.common.ResultObj;
import com.green.voluntaryfiling.domain.User;
import com.green.voluntaryfiling.service.UniversityScoreByMajorService;
import com.green.voluntaryfiling.service.UserService;
import com.green.voluntaryfiling.util.HistoryRankingByScoreUtil;
import com.green.voluntaryfiling.util.PhysicsRankingByScoreUtil;
import com.green.voluntaryfiling.util.RankingByScoreUtil;
import com.green.voluntaryfiling.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2022/03/14/23:49
 * @Description:
 */

@RequestMapping("user")

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UniversityScoreByMajorService universityScoreByMajorService;

    /**
     * 存储用户信息
     */

    @PostMapping("preserveInfo")
    public ResultObj preserveInfo(UserVo userVo){
        // 输入分数在200-750区间，否则返分数错误代码406
        try {
            if(userVo.getSt_score()>=200&&userVo.getSt_score()<=750){

                //   分数对应排位
                try {
                    //根据分数自动匹配排位位次
                    int st_score = (int) userVo.getSt_score();
                    //转换次学科数组
                    userVo.setSecond_subject(userVo.getSecondSubject()[0]);
                    userVo.setThird_subject(userVo.getSecondSubject()[1]);

                    //临时性物理转==>理科 | 历史转==>文科操作
                    if (userVo.getMainSubject().equalsIgnoreCase("历史")){
                        userVo.setSubject("文科");
                    }else {
                        userVo.setSubject("理科");
                    }
                    String subject = userVo.getSubject();
                    int rankingByScore = universityScoreByMajorService.getRankingByScore(subject, st_score);
        //   获取输入分数对应的排位   rankingByScore
                    //如果用户的排位是匹配的排位或者符合要求的排位区间，则执行，否则 返回UNIVERSITY_RANKING_MATCH_FAIL
                    if(userVo.getSt_ranking()==rankingByScore||
                            (subject.equals("理科")
                            && PhysicsRankingByScoreUtil.isRankingByScore(st_score,userVo.getSt_ranking(),rankingByScore))||
                            (subject.equals("文科")
                            && HistoryRankingByScoreUtil.isRankingByScore(st_score,userVo.getSt_ranking(),rankingByScore))){
                        try{
                            //成功匹配
                            //userVo.setSt_ranking(rankingByScore);
                            User user = userService.isExist(userVo.getUsername());
                            if (user == null){
                                this.userService.saveUser(userVo);
                                return ResultObj.USER_STORE_SUCCESS;
                            }//不存在存储用户信息
                            else{
                                this.userService.updateByUser(user,userVo);
                                return ResultObj.USER_STORE_UPDATE;
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                            return ResultObj.USER_STORE_ERROR;
                        }
                    }else{
                        return ResultObj.UNIVERSITY_RANKING_MATCH_FAIL;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    return ResultObj.UNIVERSITY_RANKING_MATCH_ERROR;
                }

            }else {

                return ResultObj.UNIVERSITY_SCORE_MATCH_FAIL;

            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UNIVERSITY_SCORE_MATCH_ERROR;
        }

    }

    /**
     * 查询用户是否存在信息
     */
    @RequestMapping("isExist")
    public ResultObj isExist(String username){



        try{
            User user = this.userService.isExist(username);
            if (user != null){  //用户存在，需要修改访问记录

                user = this.userService.updateByUser(user);
                return new ResultObj(ResultObj.USER_ISEXIST_SUCCESS,ResultObj.USER_ISEXIST_SUCCESS_MSG,user);
            }else {             //用户不存在直接返回不存在信息
                return ResultObj.USER_ISEXIST_FAIL;
            }
        }catch (Exception e){
            return ResultObj.USER_ISEXIST_ERROR;
        }
    }
}
